<?php
/*
 *   This file is part of verbum.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * 	 Copyright (c) 2008 Sergio Gabriel Teves <gabriel.sgt at gmail.com>
 */

class i18n extends entity {

	var $id;
	var $context;
	var $original;
	var $previous;
	var $translated;
	var $lang;
	var $status;
	
    public function __construct()
    {
    }

	// THIS CREATE A NEW INSTANCE OF THIS CLASS
	public function newInstance() {
		return new i18n;
	}

	// COMMON PUBLIC FUNCTIONS // NEEDED FOR LOADER
	public function get() {
		$sql = "SELECT * FROM ".I18N." WHERE id = ".util::str($this->id);
		return $sql;
	}

	public function listAll() {
		$sql = "SELECT * FROM ".I18N." ORDER BY lang, original";
		return $sql;
	}

	public function listBy($order = false) {
		$sql = "SELECT * FROM ".I18N;
		$f="";
		if ($this->context!=null) {
			$f.=" context = ".util::str($this->context);
		}
		if ($this->original!=null) {
			if ($f!="") $f .=" AND ";
			$f.=" original = ".util::str($this->original);
		}
		if ($this->lang!=null) {
			if ($f!="") $f .=" AND ";
			$f.=" lang = ".util::str($this->lang);
		}		
		if ($this->status!=null) {
			if ($f!="") $f .=" AND ";
			$f.=" status = ".util::str($this->status);
		}
		if ($f != "") $sql .= " WHERE " .$f;
		if ($order) $sql .= " ORDER BY status, original";
		return $sql;
	}	
	
	public function listAvailLang() {
		$sql = "SELECT distinct LOWER(i.lang) as lang, t.name FROM ".I18N." i INNER JOIN ".TEAMS." t ON i.lang = t.lang AND t.status = ".util::str(STATUS_ACTIVE);
		return $sql;	
	}
	
	public function find($string,$lang) {
		$sql = "SELECT * FROM ".I18N." WHERE original = ".util::str($string)." AND lang = ".util::str($lang)." AND status = ".util::str(LANG_OK)." LIMIT 0,1";
		return $sql;	
	}	
	
	public function insert() {
		$this->original = util::encode($this->original);
		$this->previous = util::encode($this->previous);
		$this->translated = util::encode($this->translated);
		$sql = "INSERT INTO ".I18N." (context, original, previous, translated, lang, status) ";
		$sql .= "VALUES (";
		$sql .=	util::str($this->context);
		$sql .=	",".util::str($this->original);
		$sql .=	",".util::str($this->previous);
		$sql .=	",".util::str($this->translated);
		$sql .=	",".util::str($this->lang);
		$sql .=	",".util::str($this->status);
		$sql .= ")";
		return $sql;
	}

	public function update() {
		$this->previous = util::encode($this->previous);
		$this->translated = util::encode($this->translated);		
		$sql = "UPDATE ".I18N;
		$sql .=	" SET translated = ".util::str($this->translated);
		$sql .=	", status = ".util::str($this->status);
		$sql .= " WHERE id = ".util::str($this->id);
		return $sql;
	}
	
	public function fillUpLang($ln) {
		$sql = "INSERT INTO ".I18N." (context, original, lang, pid) SELECT context, original, ".util::str(strtolower($ln)).", id FROM ".I18N." WHERE lang = 'en'";
		return $sql;
	}
	
	public function delete() {
		$sql = "DELETE FROM ".I18N. " WHERE id=".util::str($this->id);
		return $sql;
	}

	public function fill($array) {
		$this->id=$array['id'];
		$this->context=$array['context'];
		$this->original=util::decode($array['original']);
		$this->previous=util::decode($array['previous']);
		$this->translated=util::htmlize($array['translated']);		
		/*
		$this->original=util::decode($array['original']);
		$this->previous=util::decode($array['previous']);
		$this->translated=util::decode($array['translated']);
		*/
		$this->lang=$array['lang'];
		$this->status=$array['status'];		
	}
	
}

?>
